﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;

namespace CodeJumper
{
    public enum PluginLocation
    {
        Left = 0,
        Right,
    }

    public enum AlphabeticalSortOrder
    {
        Ascending = 0,
        Descending
    }

    public enum SortType
    {
        OriginalLayout,
        Alphabetical,
    }

    public enum YesNoType
    {
        No = 0,
        Yes
    }

    [Serializable]
    public class PluginConfiguration
    {
        private static readonly object lockObject = new object();
        private static volatile PluginConfiguration _instance;
        public AlphabeticalSortOrder AlphabeticalSortOrder = AlphabeticalSortOrder.Ascending;
        public YesNoType HideProperties = YesNoType.No;
        public YesNoType HideVariables = YesNoType.No;
        public PluginLocation PluginLocation = PluginLocation.Right;
        public SortType SortType = SortType.OriginalLayout;

        public static PluginConfiguration Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (lockObject)
                    {
                        if (_instance == null)
                        {
                            _instance = Deserialize<PluginConfiguration>();
                        }
                    }
                }
                return _instance;
            }
        }


        private static PluginConfiguration Deserialize<T>()
        {
            var x = new XmlSerializer(typeof (T));
            string modulepath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string configFile = modulepath + "\\" + "CodeJumpConfig.xml";
            try
            {
                if (File.Exists(configFile) == false)
                {
                    var config = new PluginConfiguration();
                    config.Serialize();
                }

                using (var fs = new FileStream(configFile, FileMode.Open, FileAccess.Read))
                {
                    object des = x.Deserialize(fs);
                    var inst = des as PluginConfiguration;
                    Debug.Assert(inst != null);
                    return inst;
                }
            }
            catch (Exception)
            {
                var config = new PluginConfiguration();
                return config;
            }
        }

        public void Serialize()
        {
            var x = new XmlSerializer(GetType());
            string modulepath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string configFile = modulepath + "\\" + "CodeJumpConfig.xml";
            using (var fs = new FileStream(configFile, FileMode.Create, FileAccess.ReadWrite))
            {
                x.Serialize(fs, this);
            }
        }
    }
}